package com.leet.code.lcof;

public class Code03 {

    public static void main(String[] args) {
        int[] nums = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 11};
        System.out.println(findRepeatNumber(nums));
    }

    static int findRepeatNumber1(int[] nums) {
        for (int i = 0; i < nums.length - 1; i++) {
            for (int j = i + 1; j < nums.length; j++) {
                if (nums[i] == nums[j]) {
                    return nums[i];
                }
            }
        }
        return 0;
    }

    static int findRepeatNumber(int[] nums) {
        int m;
        for (int i = 0; i < nums.length; i++) {
            if (nums[i] == i) {
                continue;
            }
            if (nums[nums[i]] == nums[i]) {
                return nums[i];
            }
            m = nums[nums[i]];
            nums[nums[i]] = nums[i];
            nums[i] = m;
            i--;
        }

        return 0;

    }
}
